import { _decorator, AudioClip, Component, director, log, Node, sys } from 'cc';
import { SSS_Consts } from "../Data/SSS_Consts";
import { SSS_GameData } from "../Data/SSS_GameData";
import { bdlMgr } from "../Manager/BundleManager_S";
import SSS_GameEventManager from "../Manager/SSS_GameEventManager";
import UI from "../Manager/UI";
import MainPanel from "../UI/MainPanel";
import StartPanel from "../UI/StartPanel";
import { SSS_AudioManager } from '../Manager/SSS_AudioManager';
import { TempSounds } from '../UI/SettingsPanel';

const { ccclass, property, executionOrder } = _decorator;

// // 慢一点执行
@ccclass('MainScene')
@executionOrder(101)
export default class MainScene extends Component {
        static Instance: MainScene;

        @property(Node)
        public UI: Node | null = null;

        ClubButton = null;

        async onLoad() {
                MainScene.Instance = this;

                //        // {
                //        //     // 获取碰撞检测系统：
                //        //     let manager = cc.director.getCollisionManager();
                //        //     // 默认碰撞检测系统是禁用的，如果需要使用则需要以下方法开启碰撞检测系统：
                //        //     manager.enabled = true;
                //        //     // 默认碰撞检测系统的 debug 绘制是禁用的，如果需要使用则需要以下方法开启 debug 绘制：
                //        //     // manager.enabledDebugDraw = true;
                //        // }

                //        // GameStorage.initData();

                SSS_GameData.InitData();
                SSS_GameData.预加载();


                TempSounds.InitMusic()

                UI.Instance.ResetUINode();

                //        // cc.director.preloadScene("GameScene");

                //        StartPanel.Instance.ShowOn();
                //        MainPanel.Instance.ShowOff();

                StartPanel.Instance.ShowOff();
                MainPanel.Instance.ShowOn();

                if (SSS_GameData.isFirstEnterMainScene) {
                        SSS_GameEventManager.Init();
                        // SSS_GameEventManager.AddListener();

                        let bgm = `bgm/To Mars And Back_Piano Loop`;
                        // let bgm = `bgm/Banjo-Menu-Loop`;
                        bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, bgm, AudioClip).then(res => {
                                SSS_AudioManager.Instance.playBGM(res, true);

                                // const audioId = audioEngine.playMusic(res, true);

                                // // 播放背景音乐并获取音频ID
                                // audioEngine.playMusic(bgmClip, true);

                                // 设置播放速度（Cocos Creator 2.4.0 + 版本支持）
                                // audioEngine.setPlaybackRate(audioId, 0.75); // 0.75倍速
                        });

                        // // let bgm = `bgm/Banjo Theme/Banjo-Gameplay-Loop`;
                        // // let bgm = `bgm/Casual Game Music 11`;
                        // let bgm = `bgm/To Mars And Back_Piano Loop`;
                        // // let bgm = `bgm/Banjo-Menu-Loop`;
                        // bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, bgm, AudioClip).then(res => {
                        //         const audioId = audioEngine.playMusic(res, true);

                        //         //                 // 播放背景音乐并获取音频ID
                        //         // audioEngine.playMusic(bgmClip, true);

                        //         // 设置播放速度（Cocos Creator 2.4.0+ 版本支持）
                        //         // audioEngine.setPlaybackRate(audioId, 0.75); // 0.75倍速

                        // });
                }
                else {
                        MainPanel.Instance.ShowOn();
                }

                SSS_GameData.isFirstEnterMainScene = false;

                director.preloadScene(SSS_Consts.SceneName.YangLeGeYang_Game_Scene);


                this.WXShare();
                this.WXGameClub_create();

                this.WXGameClub_show();
        }

        ShowOn() {
                this.node.active = true;
        }
        ShowOff() {
                this.node.active = false;
        }

        onDisable() {
                console.log(`onDisable---------`)
                this.WXGameClub_hide();
        }



        // /** 处理是否展示微信系统推荐-胜利界面每天只显示一次微信推荐*/
        // async ShowWXRecommend() {
        //         if (cc.sys.platform !== cc.sys.WECHAT_GAME) {
        //                 return
        //         }

        //         const date = Date.now();
        //         if (utils.IsOneDayPassed(GameData.getInstance().user.wxRecommendDate, date)) {
        //                 console.log(`弹出微信推荐`)
        //                 GameData.getInstance().user.wxRecommendDate = date;
        //                 GameData.getInstance().SaveData();

        //                 const pageManager = wx.createPageManager();

        //                 // 游戏场景load时
        //                 await pageManager.load({
        //                         // 推荐组件OPENLINK常量，直接复制即可，无需理解含义
        //                         openlink: 'TWFRCqV5WeM2AkMXhKwJ03MhfPOieJfAsvXKUbWvQFQtLyyA5etMPabBehga950uzfZcH3Vi3QeEh41xRGEVFw',
        //                 });
        //                 this.scheduleOnce(async () => {
        //                         await pageManager.show({
        //                                 // 推荐组件OPENLINK常量，直接复制即可，无需理解含义
        //                                 openlink: 'TWFRCqV5WeM2AkMXhKwJ03MhfPOieJfAsvXKUbWvQFQtLyyA5etMPabBehga950uzfZcH3Vi3QeEh41xRGEVFw',
        //                         });
        //                 }, 0.3)
        //         }
        //         else {
        //                 console.log(`不弹微信推荐`)
        //         }
        // }

        // 微信分享好友，分享朋友圈-系统-右上角三个点点开
        WXShare() {
                if (sys.platform == sys.Platform.WECHAT_GAME) {
                        // 微信分享好友，分享朋友圈
                        wx.showShareMenu({
                                menus: ['shareAppMessage', 'shareTimeline']
                        })
                }
        }

        // 微信游戏圈
        WXGameClub_create() {
                if (sys.platform == sys.Platform.WECHAT_GAME) {
                        this.ClubButton = wx.createGameClubButton({
                                icon: 'green',
                                type: "image",
                                text: "游戏圈",
                                style: {
                                        left: 20,
                                        top: 20,
                                        width: 40,
                                        height: 40
                                },
                                // openlink: 'Lv-XO1OgAuqztP4pRyKfZnY2aJKe9aE1'
                        })
                }
        }

        // 微信游戏圈按钮显示
        WXGameClub_show() {
                if (sys.platform == sys.Platform.WECHAT_GAME) {
                        if (this.ClubButton) {
                                this.ClubButton.show();
                        }
                }
        }

        // 微信游戏圈按钮隐藏
        WXGameClub_hide() {
                if (sys.platform == sys.Platform.WECHAT_GAME) {
                        if (this.ClubButton) {
                                this.ClubButton.hide();
                        }
                }
        }
}